package cn.iocoder.yudao.module.alipay.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * 对象工具
 */
public class ObjectUtil {
    private final static ObjectMapper OM = new ObjectMapper()
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)
            .configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);


    /**
     * 对象转json字符串
     *
     * @param value 对象
     * @return String
     */
    public static String writeValueAsString(Object value) {
        try {
            return OM.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            return "";
        }
    }

    /**
     * json字符串转对象
     *
     * @param content   json字符串
     * @param valueType class
     * @param <T>       T
     * @return T
     */
    public static <T> T readValue(String content, Class<T> valueType) {
        try {
            return OM.readValue(content, valueType);
        } catch (IOException e) {
            return null;
        }
    }

}
